package com.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pageResult.PageResult;
import com.pojo.Member;
import com.query.QueryPageBean;
import com.result.Result;
import com.service.MemberService;
import com.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Member")
public class MemberController {


    @Reference
    private MemberService memberService;



    //检查项列表(支持分页+条件查询)
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = memberService.selectByQueryString(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
    }

    //删除
    @PostMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        memberService.delete(id);
        return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }
    //添加
    @PostMapping("/add")
//    @PreAuthorize("hasAuthority('MEMBER_ADD')")
    public Result add(@RequestBody Member member){
        try {
            memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
    }

    //根据id查询
    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        try {
            Member member = memberService.queryById(id);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Member member){
        try {
            memberService.update(member);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
    }



}
